﻿@page "/Admin/Orgs"
@attribute [Authorize]
@attribute [TabItemOption(Text ="机构管理",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysOrgService SysOrgService

<div class="row h-100 overflow-hidden">
    <div class="col-2 h-100">
            <Card class ="h-100">
                <CardHeader>机构层级</CardHeader>
                <CardBody>
                    <AsyncRender OnQueryAsync="SysOrgService.GetTreeOrgsAsync" >
                        <Tree Items="context" OnTreeItemClick="OnOrgTreeItemClick"></Tree>
                    </AsyncRender>
                </CardBody>
            </Card>
    </div>

    <div class="col-10 h-100">
        <Table TItem="OrgOutput"
                @ref = "@_table"
                IsPagination="false"
                IsStriped="true"
                IsMultipleSelect="true"
                IsFixedHeader="true" 
                AllowResizing="true"
                ShowLoading="true"
                ShowColumnList="true"
                ShowCardView="true"
                ShowToolbar="true"
                ShowSearch="true"
                ShowExtendButtons="true" 
                ShowEmpty="true" 
                SearchMode="SearchMode.Top"
                SearchModel="@_search"
                EditDialogItemsPerRow="1"
                EditDialogSize="Size.Medium"
                PageItemsSource="@(new int[] { 15, 50 })"
                OnQueryAsync="@OnQueryAsync"
                OnSaveAsync="@OnSaveAsync"
                OnDeleteAsync="@OnDeleteAsync">
            <TableColumns>
                <TableColumn @bind-Field="@context.Name" />
                <TableColumn @bind-Field="@context.OrgType"/>
                <TableColumn @bind-Field="@context.Code"/>
                <TableColumn @bind-Field="@context.Sort"/>
                <TableColumn @bind-Field="@context.Remark" />
            </TableColumns>
            <EditTemplate>
                <div class="row g-3 form-inline">
                    <div class="col-sm-12">
                        <RadioList class="item-w0" @bind-Value="@context.OrgType"></RadioList>
                    </div>

                    <div class="col-sm-12">
                        <BootstrapInput @bind-Value="@context.Name" maxlength="50" />
                    </div>

                    <div class="col-sm-12">
                        <BootstrapInput @bind-Value="@context.Code" maxlength="50" />
                    </div>

                    <div class="col-sm-12">
                        <AsyncRender Context="val" OnQueryAsync="SysOrgService.GetCascaderOrgsAsync">
                            <LoadingContent>
                                <BootstrapInputLocker @bind-Value="@context.Pid" Prompt="数据加载中..."/>
                            </LoadingContent>
                            <ChildContent>
                                <Cascader @bind-Value="@context.Pid" Items="val"></Cascader>  
                            </ChildContent>
                        </AsyncRender>
                    </div>

                    <div class="col-sm-12">
                        <BootstrapInputNumber @bind-Value="@context.Sort" ShowButton="true" Max="999" Min="0" Step="5" />
                    </div>

                    <div class="col-sm-12">
                        <Textarea @bind-Value="@context.Remark" maxlength="50" placeholder="请输入备注" />
                    </div>
                </div>
            </EditTemplate>
            <SearchTemplate>
                <GroupBox Title="搜索条件">
                    <div class="row g-3 form-inline">
                        <div class="col-12 col-sm-6">
                            <BootstrapInput @bind-Value="@context.Name" ShowLabel="true"/>
                        </div>
                    </div>
                </GroupBox>
            </SearchTemplate>
        </Table>
    </div>
</div>

@code{
    private Table<OrgOutput>? _table;
    private OrgOutput _search = new();
    private long? _selectOrg;

    private Task<QueryData<OrgOutput>> OnQueryAsync(QueryPageOptions options)
    {
        var search = _search.Adapt<OrgPageInput>(options);
        search.Pid = _selectOrg;
        return SysOrgService.PageAsync(search);
    }

    private Task<bool> OnDeleteAsync(IEnumerable<OrgOutput> items)
        => SysOrgService.DeleteAsync(items);

    private async Task<bool> OnSaveAsync(OrgOutput item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysOrgService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysOrgService.EditAsync(item);
                break;
        }
        return result;
    }

    private async Task OnOrgTreeItemClick(TreeItem item)
    {
        if (item.Key != null)
        {
            _selectOrg = (long)item.Key;
            await _table!.QueryAsync();
        }
    }
}